<?php

namespace App\Http\Controllers\Api;


use App\Enum\CodeEnum;
use App\Models\VillagerUserModel;
use App\Services\ActivityApplicationService;
use App\Services\ActivityService;
use App\Services\CommonService;
use App\Services\Tools\HelpService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ActivityController extends ApiBaseController
{
    /**
     * 活动列表
     * @param Request $request
     */
    public function getActivityList(Request $request)
    {
        try {
            $commonInter = CommonService::getCommunity();

            $res = ActivityService::getActivityList($commonInter);
            return success(HelpService::resultsPage($res));
        } catch (\Exception $e) {
            return fail(CodeEnum::ERROR, $e->getMessage());
        }
    }

    public function getActivityJoinList()
    {
        try {
            $commonInter = CommonService::getCommunity();
            $res = ActivityService::getActivityJoinList($commonInter);
            return success(HelpService::resultsPage($res));
        } catch (\Exception $e) {
            return fail(CodeEnum::ERROR, $e->getMessage());
        }
    }

    /**
     * 活动详情
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getActivity(Request $request): \Illuminate\Http\JsonResponse
    {
        try {
            $res = ActivityService::getActivity($request -> id, $request -> userId,$request->appId);
            return success($res);
        } catch (\Exception $e) {
            return fail(CodeEnum::ERROR, $e->getMessage());
        }
    }

    /**
     * 活动添加
     * @param Request $request
     * @return JsonResponse|null
     */
    public function activityAdd(Request $request): ?\Illuminate\Http\JsonResponse
    {
        try {
            $user = VillagerUserModel::query()->find($request->userId);
            if( empty($user) ) return fail(CodeEnum::ERROR, '非法请求');

            $res = ActivityService::activityAdd($user, $request->post());
            if($res !== true)
                return fail(CodeEnum::ERROR, '活动添加失败');

            return success();
        }catch (\Exception $e) {
            return fail(CodeEnum::ERROR, $e->getMessage());
        }
    }

    public function activityConfirm(Request $request): ?\Illuminate\Http\JsonResponse
    {
        try {
            $res = ActivityService::activityConfirm($request->post());
            if($res !== true)
                return fail(CodeEnum::ERROR, '活动添加失败');

            return success();
        }catch (\Exception $e) {
            return fail(CodeEnum::ERROR, $e->getMessage());
        }
    }

    /**
     * 活动报名列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getActivityApplicationList(Request $request): \Illuminate\Http\JsonResponse
    {
        try {
            $res = ActivityApplicationService::getActivityApplicationList($request -> id);
            return success($res);
        } catch (\Exception $e) {
            return fail(CodeEnum::ERROR, $e->getMessage());
        }
    }

    /**
     * 活动报名
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function activityApplication(Request $request): \Illuminate\Http\JsonResponse
    {
        try {
            DB::beginTransaction();
            $res = ActivityApplicationService::activityApplication($request -> id, $request -> userId, $request -> appId);
            if($res !== true){
                DB::rollBack();
                return fail(CodeEnum::ERROR, '活动报名失败');
            }
            DB::commit();
            return success();
        } catch (\Exception $e) {
            DB::rollBack();
            return fail(CodeEnum::ERROR, $e->getMessage());
        }

    }

    /**
     * 取消报名
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function activityApplicationCancel(Request $request): \Illuminate\Http\JsonResponse
    {
        try {
            DB::beginTransaction();
            $res = ActivityApplicationService::activityApplicationCancel($request -> id, $request -> userId, $request->appId);
            DB::commit();
            return success($res);
        } catch (\Exception $e) {
            DB::rollBack();
            return fail(CodeEnum::ERROR, $e->getMessage());
        }
    }

}
